﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Sunimage.Spjg.BLL;
using Sunimage.Spjg.BLL.SysMgr;
using Sunimage.Spjg.Common;
using Sunimage.Spjg.Common.Exceptions;
using Sunimage.Spjg.Web.Helper;

namespace Sunimage.Spjg.Web.Admin
{
    public partial class File : System.Web.UI.Page
    {
        FileBl fileBl = new FileBl();
        UserBl userBl = new UserBl();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
                lblFileCount.Text = fileBl.GetTotalCount().ToString();
            }
        }

        private void LoadData()
        {
            gvUsers.DataSource = fileBl.GetAll();
            gvUsers.DataBind();
        }

        protected void gvUsers_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            //点击删除
            if (e.CommandName == "DeleteUser")
            {
                Guid id = Guid.Parse(e.CommandArgument.ToString());
                try
                {
                    fileBl.Delete(id);
                    LoadData();
                    MessageHelper.RegShowJSAjax(gvUsers, "删除文件成功", this.Page);
                }
                catch (ReferDataNotExistException)
                {
                    AlertHelper.ShowError("该文件正在被使用，不允许删除！", this.Page);
                }
            }
            else if (e.CommandName == "Download")
            {
                Guid id = Guid.Parse(e.CommandArgument.ToString());
                DiskFile file = fileBl.GetById(id);
                FileUpDownHelper.Download(file.Title, file.Path);
            }
        }

        protected void gvUsers_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            GridViewHelper.AddRowNumber(e);
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Literal ltrAuthor = e.Row.FindControl("ltrAuthor") as Literal;
                DiskFile file = e.Row.DataItem as Common.DiskFile;
                Common.User user = userBl.GetById(file.OwnerId);
                if (user != null)
                    ltrAuthor.Text = user.NickName;
                else
                    ltrAuthor.Text = AppConstant.DeleteUserNickname;
            }

        }

        protected void pager_PageChanged(object sender, EventArgs e)
        {
            gvUsers.PageIndex = pager.CurrentPageIndex - 1;
            LoadData();
        }
    }
}